//引入组件库
var do_Page = sm("do_Page");
var do_App = sm("do_App");
var nf = sm("do_Notification");
var userUrl = require("url/user");
var http = require("helper/http");
var hashdata = mm("do_HashData");

var listview = ui("listView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);

var headerAout = ui("headerAout");


//加载头部
var addheader = headerAout.add("header", "source://view/reuse/searchHeader.ui", 0, 0);
var textFieldSearch = ui(addheader + ".textFieldSearch")
var search;
textFieldSearch.on("enter", function(data, e) {
	search = this.text;
	if(null!=search&&''!=search){
		loadPage();
	}
});

//搜索按钮
var sendBtn = ui(addheader + ".sendBtn");
sendBtn.on("touch", function(data, e) {
	search = textFieldSearch.text;
	if(null!=search&&''!=search){
		loadPage();
	}
});

// andriod返回键
do_Page.on("back", function() {
	do_App.closePage();
});
// 左上角-返回
var headerLeft = ui(addheader + ".headerLeft");
headerLeft.on("touch", function() {
	do_App.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
	"searchHint" : "查找用户"
})
header.refreshData();

var noData = [{"template":1,"labelContent":"没有查找到用户!"}];
// 当前页码
var page = 1;
// 本页面-ajax业务请求url
var httpUrl = userUrl.url.base.search4AddFriend;

// 全部刷新数据
function loadPage() {
	if(null==search || ''==search){
		return;
	}
	page = 1;
	var params = {
		page : page,
		searchMore : search
	}
	http.post(httpUrl, params, function(data) {
		if (data.userPage) {
			var userList = data.userPage.content;//用户-列表
			listview.rebound();
			listdata.removeAll();// 清除数据
			if(userList.length > 0){
				listdata.addData(userList);
			}else{
				listdata.addData(noData);
			}
			listview.refreshItems();// 刷新item数据
		}
		
	});
}


// 下拉列表，刷新数据
listview.on("pull", function(data) {
	if (data.state == 2) {
		loadPage();
		listview.rebound();//复位【一定要做】
	}
});

// 上拉列表，翻页数据
listview.on("push", function(data) {
	if (data.state == 2) {
		loadPage();
		listview.rebound();//复位【一定要做】
	}
});
//页面装载完成后，开始初始化工作
do_Page.on("loaded", function(){
	loadPage();
});
